package lamma.lab.job;

import static junit.framework.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import lamma.lab2.job.AsyncStep;
import lamma.test.helper.FutureHelper;

import org.junit.Test;

/**
 * @author maxcellent
 *
 */
@Deprecated
public class ContextRunnerSingleAsyncListJob {

	private static class Job1 {
		
		private String a;
		
		private String b;

		private List<Future<String>> resultFutures;
		
		private String result;
		
		@SuppressWarnings("unused")
		public Job1(String a, String b) {
			super();
			this.a = a;
			this.b = b;
		}
		
		@SuppressWarnings("unused")
		public void unexpectedCall() {
			throw new RuntimeException("not supposed to execute");
		}
		
		@SuppressWarnings({ "unused", "unchecked" })
		@Step(serial=1)
		public List<AsyncStep> fetch() {
			resultFutures = Arrays.asList(
					FutureHelper.mockFuture(100, a), 
					FutureHelper.mockFuture(150, b), 
					FutureHelper.mockFuture(200, a), 
					FutureHelper.mockFuture(250, b));
			return AsyncStep.Builder.fromFutures(resultFutures);
		}
		
		@SuppressWarnings("unused")
		@Step(serial=2)
		public void compile() throws InterruptedException, ExecutionException {
			for (Future<String> resultFuture : resultFutures) {
				result += resultFuture.get();	
			}
		}

		@SuppressWarnings("unused")
		public String getResult() {
			return result;
		}

		@SuppressWarnings("unused")
		public void setResult(String result) {
			this.result = result;
		}
	}
	
	private class TestContext {
		
		@SuppressWarnings("unused")
		String input1;
		
		@SuppressWarnings("unused")
		String input2;
		
		@SuppressWarnings("unused")
		String input3;
		
		@FeedBy(job=Job1.class, params={"input1", "input2"}, result="result")
		String result1;

		public TestContext(String input1, String input2, String input3) {
			super();
			this.input1 = input1;
			this.input2 = input2;
			this.input3 = input3;
		}
	}
	
	@Test
	public void test() {
		TestContext c = new TestContext("a", "b", "c");
		
		long start = System.currentTimeMillis();
		
		ContextRunner runner = new ContextRunner();
		Result result = runner.run(c);
		
		long end = System.currentTimeMillis();
		System.out.println(end - start);
		
		assertEquals(Result.Status.SUCCESS, result.getStatus());
		assertEquals("nullabab", c.result1);
	}
}

